<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>

<!-- 更新商品分类添加弹出层 -->
<div id="update-user-layer" style="display: none; padding: 20px">
    <form id="update-user-form" class="layui-form layui-form-pane" lay-filter="update-user-form">
        <div class="layui-form-item">
            <label class="layui-form-label">商品类名</label>
            <div class="layui-input-block">
                <input type="text" name="category" id="category" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新增商品ID</label>
            <div class="layui-input-block">
                <input type="text" name="goodsId" id="goodsId" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="update-user-form-submit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<!-- 更新商品分类删除弹出层 -->
<div id="update-user-layer1" style="display: none; padding: 20px">
    <form id="update-user-form1" class="layui-form layui-form-pane" lay-filter="update-user-form1">
        <div class="layui-form-item">
            <label class="layui-form-label">商品类名</label>
            <div class="layui-input-block">
                <input type="text" name="category" id="category1" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">删除商品ID</label>
            <div class="layui-input-block">
                <input type="text" name="goodsId" id="goodsId1" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="update-user-form-submit1">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="demoTable">
            搜索商品类名：
            <div class="layui-inline">
                <input class="layui-input" name="catgory" id="demoReload1" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
        </div>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="classifications" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">添加商品</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit1">删除商品</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            classifications = layui.table;

        classifications.render({
            elem: '#classifications',
            url:'/category/getAllclassification',
            toolbar: '#toolbarDemo',
            title:'商品分类',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'category', width: 120, title: '商品类名'},
                {
                    field: 'goodsName',
                    width: 500,
                    title: '商品名称',
                    templet: function (d) {
                        var series1 = [];
                        $.each(d.goods, function (index, item) {
                            series1.push(item.goodsName);
                        });
                            return series1;
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            id: 'testReload',
        });
        var $ = layui.$, active = {
            reload: function() {

                var demoReload1 = $('#demoReload1');

                classifications.reload('testReload', {
                    url: '/category/getByCatgory',
                    where: {
                        category: demoReload1.val()
                    }
                }, 'data');

            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /**
         * toolbar监听事件
         */
        classifications.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加商品分类',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/newClassification.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        classifications.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        classifications.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                // 每次显示更新商品分类的表单前自动为表单填写该行的数据
                form.val('update-user-form', {
                    "category": data.category
                });
                // 显示更新商品分类表单的弹出层
                layer.open({
                    type: 1,
                    title: '编辑商品分类',
                    skin: 'layui-layer-molv',
                    area: ['500px'],
                    content: $('#update-user-layer')
                });
                // 更新商品分类表单提交
                form.on('submit(update-user-form-submit)', function(data) {
                    var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
                    console.log(param);//测试是否获取到表单数据，调试模式下在页面控制台查看
                    var a={
                        "category":param.category,
                        "goods":[{"goodsId":param.goodsId}]
                    }
                    // ajax方式更新商品
                    $.ajax({
                        url: "/category/modifyClassification",
                        type:"PUT",
                        // data: JSON.stringify(data.field),
                        data:JSON.stringify(a),
                        contentType: 'application/json;charset=utf-8',
                        dataType: 'json',
                        success: function(data) {
                            if (data.status == 1) {
                                layer.close(layer.index);
                                layer.msg('更新成功');
                                classifications.reload('testReload');
                            } else {
                                layer.msg('更新失败');
                            }
                        },
                        error: function() {
                            console.log("ajax error");
                        }
                    });
                    // 阻止表单跳转
                    return false;
                });

            }
            if (obj.event === 'edit1') {
                // 每次显示更新商品分类的表单前自动为表单填写该行的数据
                form.val('update-user-form1', {
                    "category": data.category
                });
                // 显示更新商品分类表单的弹出层
                layer.open({
                    type: 1,
                    title: '编辑商品分类',
                    skin: 'layui-layer-molv',
                    area: ['500px'],
                    content: $('#update-user-layer1')
                });
                // 更新商品分类表单提交
                form.on('submit(update-user-form-submit1)', function(data) {
                    var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
                    console.log(param);//测试是否获取到表单数据，调试模式下在页面控制台查看
                    var a={
                        "category":param.category,
                        "goods":[{"goodsId":param.goodsId}]
                    }
                    // ajax方式更新商品
                    $.ajax({
                        url: "/category/deleteByCategoryAndGoods",
                        type:"PUT",
                        // data: JSON.stringify(data.field),
                        data:JSON.stringify(a),
                        contentType: 'application/json;charset=utf-8',
                        dataType: 'json',
                        success: function(data) {
                            if (data.status == 1) {
                                layer.close(layer.index);
                                layer.msg('更新成功');
                                classifications.reload('testReload');
                            } else {
                                layer.msg('更新失败');
                            }
                        },
                        error: function() {
                            console.log("ajax error");
                        }
                    });
                    // 阻止表单跳转
                    return false;
                });

            }
            if (obj.event === 'delete') {
                layer.confirm('确定删除该商品分类吗', function (index) {
                    $.ajax({
                        url:"/category/deleteByCategory",
                        data:{"category":data.category},
                        success:function (data) {
                            if(data.status==1){
                                layer.msg("删除成功");
                                obj.del();
                            }else {
                                layer.msg("删除失败");
                            }

                        }
                    })
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>